#include "SPopupWindow.h"
#include <core/StyleMgr.h>
#include <core/ImageMgr.h>
#include "SImageButton.h"
#include <QPushButton>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QStylePainter>
#include <QVBoxLayout>
#include <QHBoxLayout>

SPopupWindow::SPopupWindow(QWidget* parent /*= nullptr*/)
	: QDialog(parent)
{
	setWindowFlag(Qt::FramelessWindowHint);
	setAttribute(Qt::WA_TranslucentBackground);

	QVBoxLayout* layout = new QVBoxLayout();//窗口布局
	layout->setContentsMargins(0, 0, 0, 0);
	layout->setSpacing(0);

	QHBoxLayout* titleLayout = new QHBoxLayout();//标题栏
	titleLayout->setSpacing(0);
	titleLayout->setContentsMargins(0, 1, 1, 0);
	titleLayout->setAlignment(Qt::AlignTop);
	layout->addLayout(titleLayout);

	QWidget* dumyWidget = new QWidget(this);
	dumyWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	dumyWidget->setFixedHeight(24);
	titleLayout->addWidget(dumyWidget);

	SImageButton* btnClose = new SImageButton(this, "widgets_win_close");
	titleLayout->addWidget(btnClose);

	QVBoxLayout* layoutMain = new QVBoxLayout(); //要显示的元素布局,包含内容控件和主要的按钮
	layout->addLayout(layoutMain);
	layoutMain->setContentsMargins(8, 0, 8, 8);
	layoutMain->setSpacing(0);

	m_contentLayout = new QVBoxLayout();
	m_contentLayout->setContentsMargins(0, 0, 0, 0);
	m_contentLayout->setSpacing(0);
	layoutMain->addLayout(m_contentLayout);
	layoutMain->addStretch();//撑起来

	m_btnLayout = new QHBoxLayout();
	m_btnLayout->setContentsMargins(0, 0, 0, 0);
	m_btnLayout->setSpacing(8);
	m_btnLayout->setDirection(QBoxLayout::RightToLeft);
	layoutMain->addLayout(m_btnLayout);

	

	setLayout(layout);

	connect(btnClose, &SImageButton::clicked, this, &QDialog::reject);
}

void SPopupWindow::setTitle(const QString& v)
{
	m_title = v;
}

QPushButton* SPopupWindow::addBtn(QMessageBox::StandardButton btnType, const QString& caption)
{
	Q_ASSERT_X(m_btnLayout->isEmpty(), "SPopupWindow::addBtn", "请在显示前添加！");
	if (!m_btnLayout->isEmpty())//已经布局，不能再添加
		return nullptr;

	switch (btnType)
	{
	case QMessageBox::Ok:
		m_btnOK = new QPushButton(caption, this);
		m_btnOK->setFixedSize(120, 24);//TODO: check size?
		return m_btnOK;
		break;
	case QMessageBox::Cancel:
		m_btnCancel = new QPushButton(caption, this);
		m_btnCancel->setObjectName("grey");
		m_btnCancel->setFixedSize(80, 24);
		return m_btnCancel;
		break;
	case QMessageBox::Apply:
		m_btnApply = new QPushButton(caption, this);
		m_btnApply->setFixedSize(80, 24);
		return m_btnApply;
		break;
	default:
		Q_ASSERT_X(m_btnLayout->isEmpty(), "SPopupWindow::addBtn", "仅支持OK Cancel, Apply！");
		break;
	}

	return nullptr;
}

static QPoint s_oldPos;
void SPopupWindow::keyPressEvent(QKeyEvent* e)
{
	if (e->key() == Qt::Key_Escape)
		reject();
}

void SPopupWindow::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		setCursor(Qt::SizeAllCursor);
		s_oldPos = event->globalPos();
		m_dragging = true;
	}
}

void SPopupWindow::mouseReleaseEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		setCursor(Qt::ArrowCursor);
		m_dragging = false;
	}
}

void SPopupWindow::mouseMoveEvent(QMouseEvent* event)
{
	if (event->buttons() & Qt::LeftButton && m_dragging)
	{
		QPoint offset = event->globalPos() - s_oldPos;
		move(frameGeometry().topLeft() + offset);
		s_oldPos = event->globalPos();
	}
}

void SPopupWindow::paintEvent(QPaintEvent* event)
{
	QBrush brush(StyleMgr::instance().color("BasicPlate"));
	QPen pen(StyleMgr::instance().color("BorderNormal"));
	int r = StyleMgr::instance().size("RoundCorner");
	
	QStylePainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setBrush(brush);
	painter.setPen(pen);
	painter.drawRoundedRect(rect(), r, r);

	QRect titleR = rect();
	titleR.setHeight(24);
	painter.setPen(StyleMgr::instance().color("TextNormal"));
	painter.drawText(titleR, m_title, QTextOption(Qt::AlignCenter));

}

void SPopupWindow::resizeEvent(QResizeEvent*)
{
	if (!m_btnLayout->isEmpty())
		return;

	if (m_btnOK)
		m_btnLayout->addWidget(m_btnOK);
	if (m_btnCancel)
		m_btnLayout->addWidget(m_btnCancel);
	m_btnLayout->addStretch();
	if (m_btnApply)
		m_btnLayout->addWidget(m_btnApply);
}
